//
//  HMaskWindowManager.swift
//  Drop
//
//  Created by hushaohua on 2017/11/16.
//  Copyright © 2017年 init. All rights reserved.
//

import UIKit

class HMaskWindowManager: NSObject, UIGestureRecognizerDelegate {
    lazy var window:UIWindow = {
        var window:UIWindow = UIWindow(frame: UIScreen.main.bounds);
        window.alpha = 0;
        window.isHidden = true;
        return window;
    }();
    
    override init() {
        super.init();
        let gesture:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapWindow));
        gesture.delegate = self;
        self.window.addGestureRecognizer(gesture);
    }
    
    var keyboardDismissClosure:(() -> ())?;
    var keyboardDidAppear:Bool = false;
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        let point = touch.location(in: self.window);
        if (point.y < (self.backgroundView?.frame.origin.y)!){
            return true;
        }
        return false;
    }
    
    @objc func tapWindow() -> Void {
        self.dismiss();
    }
    
    func visualEffectView(size:CGSize) -> (UIVisualEffectView, UIVisualEffectView) {
        let blurEffect = UIBlurEffect(style:.light);
        let blurView:UIVisualEffectView = UIVisualEffectView(effect: blurEffect);
        blurView.frame.size = size;
        let vibrancyEffect = UIVibrancyEffect(blurEffect:blurEffect);
        let vibrancyView = UIVisualEffectView(effect:vibrancyEffect);
        vibrancyView.frame.size = size;
        blurView.contentView.addSubview(vibrancyView);
        return (blurView, vibrancyView);
    }
    
    var backgroundView:UIView?;
    var mainView:UIView?{
        didSet{
            oldValue?.removeFromSuperview();
            if (self.mainView != nil){
                self.loadMainView();
            }
        }
    };
    
    static let sharedInstance = HMaskWindowManager();
    class func sharedMaskForView(_ view:UIView) -> HMaskWindowManager? {
        if (sharedInstance.mainView == nil){
            sharedInstance.mainView = view;
        }
        return sharedInstance;
    }
    
    func loadMainView() -> Void {
        self.backgroundView?.removeFromSuperview();
        
        var mainFrame:CGRect = .zero;
        mainFrame.origin.y = self.window.frame.size.height;
        mainFrame.origin.x = 0;
        mainFrame.size.width = self.window.frame.size.width;
        mainFrame.size.height = self.mainView!.frame.size.height;
        
        self.backgroundView = UIView(frame:mainFrame);
        
        let (blurView, vibrancyView) = self.visualEffectView(size: self.mainView!.frame.size);
        blurView.frame = self.backgroundView!.bounds;
        vibrancyView.frame = blurView.bounds;
        self.backgroundView?.addSubview(blurView);
        self.backgroundView?.addSubview(self.mainView!);
        self.window.addSubview(self.backgroundView!);
    }
    
    func dismiss() -> Void {
        if (self.keyboardDidAppear && self.keyboardDismissClosure != nil){
            self.keyboardDismissClosure!();
            self.removeKeyboardObserver();
        }else{
            self.removeKeyboardObserver();
            self.dismissOnlyMainView();
        }
    }
    
    func dismissOnlyMainView() -> Void{
        UIView.animate(withDuration: 0.2, animations: {
            var frame = self.backgroundView!.frame;
            frame.origin.y = self.window.frame.size.height;
            self.backgroundView!.frame = frame;
            //            self.mainView!.frame = frame;
            self.window.alpha = 0;
        }) { (finished) in
            self.clearHolderViews();
        };
    }
    
    func clearHolderViews() -> Void{
        self.window.isHidden = true;
        self.backgroundView = nil;
        self.mainView = nil;
    }
    
    func present() -> Void {
        self.window.isHidden = false;
        self.window.backgroundColor = UIColor(white:0.5, alpha:0.5);
        UIView.animate(withDuration: 0.2, animations: {
            self.window.alpha = 1;
            var frame = self.backgroundView!.frame;
            frame.origin.y = self.window.frame.size.height - frame.size.height;
            self.backgroundView!.frame = frame;
        }) { (finished) in
            self.addKeyboardObserver();
        };
    }
}
